package com.example.springbatchdemo.config.db;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.JdbcPagingItemReader;
import org.springframework.batch.item.database.Order;
import org.springframework.batch.item.database.support.MySqlPagingQueryProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.RowMapper;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

/**
 * @Classname ItemReaderDbDemo
 * @Description TODO
 * @Date 2020/9/19 15:43
 * @Created by kevin
 */

@Configuration
public class ItemReaderDbDemo {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    @Autowired
    private DataSource dataSource;
    @Autowired
    private DbJdbcWriter dbJdbcWriter;

    @Bean
    public Job itemReaderDbJob() {
        return jobBuilderFactory.get("itemReaderDbJob")
                .start(itemReaderDbStep())
                .build();
    }

    @Bean
    public Step itemReaderDbStep() {
        return stepBuilderFactory.get("itemReaderDbStep")
                .<User, User>chunk(2)
                .faultTolerant()
                .reader(dbJdbcReadder())
                .writer(dbJdbcWriter)
                .build();
    }


    @Bean
    @StepScope
    public JdbcPagingItemReader<User> dbJdbcReadder() {

        JdbcPagingItemReader<User> reader = new JdbcPagingItemReader<>();
        //设置数据源
        reader.setDataSource(dataSource);
        //一次提交的数量
        reader.setFetchSize(2);
        //把读取的数据转换为对应的对象
        reader.setRowMapper(new RowMapper<User>() {
            @Override
            public User mapRow( ResultSet resultSet, int i ) throws SQLException {
                User user = new User();
                user.setId(resultSet.getInt(1));
                user.setUsername(resultSet.getString(2));
                user.setPassword(resultSet.getString(3));
                user.setAge(resultSet.getInt(4));
                return user;
            }
        });
        // 指定SQL语句
        MySqlPagingQueryProvider provider = new MySqlPagingQueryProvider();
        // 指定查询的字段
        provider.setSelectClause("id,username,password,age");
        // 指定查询的表
        provider.setFromClause("from user");

        //指定排序的字段
        HashMap<String, Order> sort = new HashMap<>(1);
        sort.put("id", Order.ASCENDING);
        provider.setSortKeys(sort);

        reader.setQueryProvider(provider);
        return reader;
    }
}
